﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FRMS.BUS;
using FRMS.DAL;
using FRMS.Model;
using FRMS.TimedTask;
using Quartz;
using Quartz.Impl;

namespace FRMS
{
    public class JobSchedulerWork
    {
        public static async void Work()
        {
            //调度器工厂
            ISchedulerFactory Work = new StdSchedulerFactory();
            //调度器
            IScheduler schedulers_work = await Work.GetScheduler();
            await schedulers_work.GetJobGroupNames();
            /*-------------计划任务代码实现------------------*/
            //创建任务
            IJobDetail job_Work = JobBuilder.Create<WorkAbnormalAppService>()
                .WithIdentity("TimeTrigger", "TimeGroup")
                .Build();
            //创建触发器 2点一分时被执行
            //ITrigger trigger9 = TriggerBuilder.Create().WithCronSchedule("0 01 02 * * ?").WithIdentity("TimeTriggerddd", "TimeGroupdd").Build();
            //每隔多久执行一次  这个是每隔多久执行一遍
            ITrigger trigger = TriggerBuilder.Create().WithIdentity("TimeTrigger", "TimeGroup").WithSimpleSchedule(t => t.WithIntervalInSeconds(60).RepeatForever()).Build();//间隔60秒执行一次任务
            //添加任务及触发器至调度器中
            await schedulers_work.ScheduleJob(job_Work, trigger);
            /*-------------计划任务代码实现------------------*/
            //启动
            await schedulers_work.Start();
        }
        public static async void CleanData()
        {
            //调度器工厂
            ISchedulerFactory Work = new StdSchedulerFactory();
            //调度器
            IScheduler schedulers_work = await Work.GetScheduler();
            await schedulers_work.GetJobGroupNames();
            /*-------------计划任务代码实现------------------*/
            //创建任务
            IJobDetail job_Work = JobBuilder.Create<DataCleanService>()
                .WithIdentity("TimeTrigger1", "TimeGroup1")
                .Build();
            //创建触发器 0点1分时被执行
            ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("0 05 01 * * ?").WithIdentity("TimeTriggerddd1", "TimeGroupdd1").Build();
            //每隔多久执行一次  这个是每隔多久执行一遍
            //ITrigger trigger = TriggerBuilder.Create().WithIdentity("TimeTrigger", "TimeGroup").WithSimpleSchedule(t => t.WithIntervalInSeconds(60).RepeatForever()).Build();//间隔60秒执行一次任务
            //添加任务及触发器至调度器中
            await schedulers_work.ScheduleJob(job_Work, trigger);
            /*-------------计划任务代码实现------------------*/
            //启动
            await schedulers_work.Start();
        }
        public static async void Register()
        {
            //调度器工厂
            ISchedulerFactory Work = new StdSchedulerFactory();
            //调度器
            IScheduler schedulers_work = await Work.GetScheduler();
            await schedulers_work.GetJobGroupNames();
            /*-------------计划任务代码实现------------------*/
            //创建任务
            IJobDetail job_Work = JobBuilder.Create<ReregisterService>()
                .WithIdentity("TimeTrigger2", "TimeGroup2")
                .Build();
            //创建触发器 2点一分时被执行
            ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("0 01 04 * * ?").WithIdentity("TimeTriggerddd2", "TimeGroupdd2").Build();
            //每隔多久执行一次  这个是每隔多久执行一遍
            //ITrigger trigger = TriggerBuilder.Create().WithIdentity("TimeTrigger", "TimeGroup").WithSimpleSchedule(t => t.WithIntervalInSeconds(60).RepeatForever()).Build();//间隔60秒执行一次任务
            //添加任务及触发器至调度器中
            await schedulers_work.ScheduleJob(job_Work, trigger);
            /*-------------计划任务代码实现------------------*/
            //启动
            await schedulers_work.Start();
        }
        public static void ResetState()
        {
            try
            {
                DeviceDAL deviceDAL = new DeviceDAL();
                RecordDAL recordDAL = new RecordDAL();
                List<DeviceInfo> list = deviceDAL.QueryOnLine();
                if (list != null)
                {
                    foreach (var item in list)
                    {
                        try
                        {
                            recordDAL.InsertDeviceRecord(item.DeviceUuid, item.DeviceNO, item.DeviceIP, "离线");
                        }
                        catch
                        {
                            continue;
                        }
                    }
                    deviceDAL.ResetState();
                }

            }
            catch
            {
                return;
            }
        }
        public static async void SyncData()
        {
            //调度器工厂
            ISchedulerFactory Work = new StdSchedulerFactory();
            //调度器
            IScheduler schedulers_work = await Work.GetScheduler();
            await schedulers_work.GetJobGroupNames();
            /*-------------计划任务代码实现------------------*/
            //创建任务
            IJobDetail job_Work = JobBuilder.Create<SyncService>()
                .WithIdentity("TimeTrigger_Sync", "TimeGroup_Sync")
                .Build();
            //创建触发器 0点1分时被执行
            //ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("0 05 01 * * ?").WithIdentity("TimeTriggerddd1", "TimeGroupdd1").Build();
            //每隔多久执行一次  这个是每隔多久执行一遍
            ITrigger trigger = TriggerBuilder.Create().WithIdentity("TimeTrigger_Sync", "TimeGroup_Sync").WithSimpleSchedule(t => t.WithIntervalInSeconds(60).RepeatForever()).Build();//间隔60秒执行一次任务
            //添加任务及触发器至调度器中
            await schedulers_work.ScheduleJob(job_Work, trigger);
            /*-------------计划任务代码实现------------------*/
            //启动
            await schedulers_work.Start();
        }
        public static async void GetAuditData()
        {
            //调度器工厂
            ISchedulerFactory Work = new StdSchedulerFactory();
            //调度器
            IScheduler schedulers_work = await Work.GetScheduler();
            await schedulers_work.GetJobGroupNames();
            /*-------------计划任务代码实现------------------*/
            //创建任务
            IJobDetail job_Work = JobBuilder.Create<GetAuditData>()
                .WithIdentity("TimeTrigger_GetAuditData", "TimeGroup_GetAuditData")
                .Build();
            //创建触发器 0点1分时被执行
            //ITrigger trigger = TriggerBuilder.Create().WithCronSchedule("0 05 01 * * ?").WithIdentity("TimeTriggerddd1", "TimeGroupdd1").Build();
            //每隔多久执行一次  这个是每隔多久执行一遍
            ITrigger trigger = TriggerBuilder.Create().WithIdentity("TimeTrigger_GetAuditData", "TimeGroup_GetAuditData").WithSimpleSchedule(t => t.WithIntervalInSeconds(60).RepeatForever()).Build();//间隔60秒执行一次任务
            //添加任务及触发器至调度器中
            await schedulers_work.ScheduleJob(job_Work, trigger);
            /*-------------计划任务代码实现------------------*/
            //启动
            await schedulers_work.Start();
        }
    }
}
